#pragma once
#include <QWidget>
#include <memory>
#include <thread>
#include <qmutex.h>
#include <QMutexLocker>
#include <qpainter.h>
#include <qqueue.h>
#include <qevent.h>
#include <wrl/client.h>
#ifdef ENABLE_LIB_MEDIA
#include "ffmpeg_stream.h"
#include "ffmpeg_decoder.h"
#include "ffmpeg_helper.h"
#include "ffmpeg_d3d_decoder.h"
#include "ffmpeg_dxva_decoder.h"
#include "d3d11_renderer.h"
#include "dxva_renderer.h"
#endif

using namespace HSDK;

namespace Ui {
    class PlayerWidget;
}

class SettingsWidget;

class PlayerWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PlayerWidget(QWidget* parent = nullptr);
    ~PlayerWidget();

protected:
    void paintEvent(QPaintEvent* event) override;

private slots:
    void slot_btn_open();
    void slot_btn_close();
    void slot_btn_settings();

    void recreate_window_widget();

private:
    void init_soft();
    void init_d3d();
    void init_dxva();

    bool render_soft_frame(std::shared_ptr<AVFrame> frame);
    
private:
    Ui::PlayerWidget* ui;
    std::shared_ptr<std::thread> thread_;
    bool is_stopped_;

    std::shared_ptr<FFmpegStream> stream_;
    std::shared_ptr<FFmpegVideoDecoder> decoder_;
    std::shared_ptr<D3D11Renderer> d3d_renderer_;
    std::shared_ptr<DxvaRenderer> dxva_renderer_;

    std::shared_ptr<SettingsWidget> settings_;
    bool is_show_settings_;

    // 增加帧队列和互斥锁
    QQueue<std::shared_ptr<AVFrame>> frame_queue_;
    QMutex PlayerWidget::queue_mutex_;

    Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
    Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_context_;
};